clear
clear matrix
set more off

* ______________________________________________________________________________
* REGRESSIONS FOR TABLE 5

* ==============================================================================
* full panel regressions
* ==============================================================================
use "[ENERGY DATA - FULL PANEL]", clear
keep if conversions == 0 | (conversions == 1 & wg == "SF")

gen log_euse = log(euse)

* generate pre-period indicator
gen pre = 0
replace pre = 1 if ((year == year(appdate) & month >= month(appdate)) | ///
(year > year(appdate))) & tau < 0

* generate proxy application and enrollment dates
local proxy = 2.5
gen proxy_tau = 12*(year-event_year + `proxy') + month - event_month ///
	if conversions == 1
gen proxy_appdate = appdate - 365*`proxy'
format proxy_appdate %tdnn/dd/CCYY

gen proxy_chi = 0
replace proxy_chi = 1 if proxy_tau >= 0 & proxy_tau !=.

gen proxy_pre = 0
replace proxy_pre = 1 if ((year == year(proxy_appdate) ///
	& month >= month(proxy_appdate)) ///
	| (year > year(proxy_appdate))) & proxy_tau < 0

* generate zero consumption indicators
by id: egen minwuse = min(wuse)
gen zero = 0
replace zero = 1 if minwuse == 0
la var zero "1 if parcel contains zero water use anywhere"

by id: egen mineuse = min(euse)
gen zero_e = 0
replace zero_e = 1 if mineuse == 0
la var zero_e "1 if parcel contains zero energy use anywhere"

tab conversions, missing
tab conversions if zero == 0, missing
tab conversions zero, missing
tab balanced conversions if zero == 0, missing
tab cohort2 conversions if zero==0, missing

* define local fixed effect specification variables
local fe_pmFEymc2FE "i.id#i.month i.year#i.month#i.cohort2"

local fixedeffect "pmFEymc2FE"
local depvar "euse log_euse"
foreach dv of local depvar {
foreach fe of local fixedeffect {

display in red "******* main specification: dep var is `dv'"
reghdfe `dv' chi pre, a(`fe_`fe'') pool(1) vce(cl id)
	local regid "`dv'_`fe'"
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_did_`regid'.ster", replace
	
display in red "******* Falsification test: dep var is `dv'"
reghdfe `dv' proxy_chi proxy_pre if conversions == 0 ///
	| (conversions == 1 & tau < -18), a(`fe_`fe'') pool(1) vce(cl id)
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_did_`dv'_`fe'proxy.ster", replace

}
* define preferred fixed-effect specification for remaining regressions
local fe "pmFEymc2FE"

display in red "******* 6-month donut: dep var is `dv'"
reghdfe `dv' chi pre if (tau<-6 | tau>6), a(`fe_`fe'') pool(1) vce(cl id)
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_did_`dv'_`fe'6moD.ster", replace

display in red "******* no zero: dep var is `dv'"
reghdfe `dv' chi pre if zero == 0 & zero_e == 0, a(`fe_`fe'') pool(1) vce(cl id)
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_did_`dv'_`fe'noZero.ster", replace

display in red "******* chi-by-tau^2: dep var is `dv'"
reghdfe `dv' chi pre chiBYtau chiBYtau2, a(`fe_`fe'') pool(1) vce(cl id)
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_did_`dv'_`fe'tau2.ster", replace	

}

* ==============================================================================
* matched panel regressions
* ==============================================================================

use "[ENERGY DATA - MATCHED PANEL]", clear

by id: egen mineuse = min(euse)
gen zero_e = 0
replace zero_e = 1 if mineuse == 0
la var zero_e "1 if parcel contains zero energy use anywhere"

gen log_euse = log(euse)

* generate proxy application and enrollment dates
local proxy = 2.5
gen proxy_tau = 12*(year-event_year + `proxy') + month - event_month ///
	if conversions == 1
gen proxy_appdate = appdate - 365*`proxy'
format proxy_appdate %tdnn/dd/CCYY

gen proxy_chi = 0
replace proxy_chi = 1 if proxy_tau >= 0 & proxy_tau !=.

gen proxy_pre = 0
replace proxy_pre = 1 if ((year == year(proxy_appdate) ///
	& month >= month(proxy_appdate)) ///
	| (year > year(proxy_appdate))) & proxy_tau < 0

* define local fixed effect specification variables
local fe_pmFEymFE   "i.id#i.month i.year#i.month"

local fixedeffect "pmFEymFE"
local depvar "euse log_euse"
foreach dv of local depvar {
foreach fe of local fixedeffect {
display in red "******* main specification: dep var is `dv'"
reghdfe `dv' chi pre [fweight=parcelfreq], a(`fe_`fe'') pool(1) vce(cl id)
	local regid "`dv'_`fe'"
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_didmatch_`regid'.ster", replace
	
display in red "******* Falsification test: dep var is `dv'"
reghdfe `dv' proxy_chi proxy_pre if conversions == 0 ///
	| (conversions == 1 & tau < -18) [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl id)
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_didmatch_`dv'_`fe'proxy.ster", replace
}
* define preferred fixed-effect specification for remaining regressions
local fe "pmFEymFE"

display in red "******* 6-month donut: dep var is `dv'"
reghdfe `dv' chi pre if (tau<-6 | tau>6) [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl id)
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_didmatch_`dv'_`fe'6moD.ster", replace

display in red "******* no zero: dep var is `dv'"
reghdfe `dv' chi pre if zero == 0 & zero_e == 0 [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl id)
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_didmatch_`dv'_`fe'noZero.ster", replace


display in red "******* chi-by-tau^2: dep var is `dv'"
reghdfe `dv' chi pre chiBYtau chiBYtau2 [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl id)
	display "command line: `e(cmdline)'"
	estimates save "$output/ep1_didmatch_`dv'_`fe'tau2.ster", replace
}